package achen;
import java.sql.*;

@SuppressWarnings("Duplicates")
public class init {

    private static final String URL_MEM = "jdbc:h2:~/homework;MODE=MYSQL;DB_CLOSE_DELAY=-1";

    private static final String DRIVER = "org.h2.Driver";

    private static final String NAME = "sa";
    private static final String PASSWORD = "sa";

    public static void main(String[] args) {

        try (Connection conn = DriverManager.getConnection(URL_MEM, NAME, PASSWORD)) {

            Statement stmt = conn.createStatement();

            stmt.executeUpdate(
                    "CREATE TABLE IF NOT EXISTS `student` (" +
                            "  `id` int(11) unsigned NOT NULL AUTO_INCREMENT," +
                            "  `course_name` varchar(100) NOT NULL," +
                            "  `grade` int(11) NOT NULL," +
                            "  PRIMARY KEY (`id`)" +
                            ");"
            );

            // 插入数据记录
            stmt.executeUpdate(
                    "insert into `student` (`id`,`course_name`,`grade`) values (1, '语文', 100);" +
                            "insert into `student` (`id`,`course_name`,`grade`) values (2, '数学', 100);"+
                    "insert into `student` (`id`,`course_name`,`grade`) values (3, '英语', 100);"
            );

            ResultSet rs = stmt.executeQuery("select * from student");//选择import java.sql.ResultSet;

            // 遍历每行记录
            while (rs.next()) {
                //如果结果集中有数据，就会循环打印出来
                System.out.println(rs.getString("id") + "," + rs.getString("course_name")+","+rs.getString("grade")  );
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

